package com.xiake.zojcodesandbox.docker;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.*;
import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.api.model.PullResponseItem;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.command.LogContainerResultCallback;

import java.util.List;

public class DockerDemo {
    public static void main(String[] args) throws InterruptedException {

//        //执行Docker的ping命令
        DockerClient dockerClient = DockerClientBuilder.getInstance().build();
        dockerClient.pingCmd().exec();
//
        //拉取镜像
        String image = "nginx:latest";
        PullImageCmd pullImageCmd = dockerClient.pullImageCmd(image);
        PullImageResultCallback pullImageResultCallback = new PullImageResultCallback(){
            @Override
            public void onNext(PullResponseItem item) {
                System.out.println("下载镜像状态:" + item.getStatus());
                super.onNext(item);
            }
        };
        pullImageCmd.
                exec(pullImageResultCallback)
                .awaitCompletion(); // 等待下载完成，不然一直阻塞

        System.out.println("下载完成");

    //创建容器
        CreateContainerCmd containerCmd = dockerClient.createContainerCmd(image);
        CreateContainerResponse containerResponse = containerCmd
                .withCmd("echo", "create sucess!")
                .exec();
        String id = containerResponse.getId();

        //查看容器列表
        ListContainersCmd listContainersCmd = dockerClient.listContainersCmd();
        List<Container> list = listContainersCmd.withShowAll(true).exec();
        for (Container container : list) {
            System.out.println(container);
        }

        //启动容器
       dockerClient.startContainerCmd(id).exec();

        //查看日志
        LogContainerCmd logContainerCmd = dockerClient.logContainerCmd(id);
        LogContainerResultCallback logContainerResultCallback = new LogContainerResultCallback(){
            @Override
            public void onNext(Frame item) {
                System.out.println("日志："+new String(item.getPayload()));
                super.onNext(item);
            }
        };
        logContainerCmd
                .withStdOut(true)      //必须开启
                .withStdErr(true)
                .exec(logContainerResultCallback)
                .awaitCompletion();    //异步等待

        //删除容器
        dockerClient.removeContainerCmd(id).exec();

        //删除镜像
        dockerClient.removeImageCmd(image).exec();

        //删除所有容器
//        ListContainersCmd listContainersCmd = dockerClient.listContainersCmd();
//        List<Container> list = listContainersCmd.withShowAll(true).exec();
//        for (Container container : list) {
//            String id = container.getId();
//            dockerClient.removeContainerCmd(id)
//                    .withForce(true)
//                    .exec();
//        }
//        System.out.println("删除成功！");
    }
}
